/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.co.drpj.midi.voice;

import java.util.Observable;
import java.util.Properties;
import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.Receiver;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Synthesizer;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import uk.co.drpj.Config;

/**
 *
 * @author pjl
 */
public class PatchSelector  extends Observable {

    JComponent patchPanel;
    VoiceTree vTree;
    private Receiver recv;
	private int chan;

    public PatchSelector(Synthesizer synth,Receiver recv,int chan) {
        PatchNameMap pmap = new PatchNameMap(synth, chan);
        patchPanel = createVoiceTree(pmap);
        this.recv=recv;
        this.chan=chan;
    }

    JComponent createVoiceTree(PatchNameMap vList) {


        vTree = new VoiceTree(vList);
        JComponent ret = new JScrollPane(vTree);
     //   ret.setPreferredSize(new Dimension(9000, 100));
        vTree.addTreeSelectionListener(new TreeSelectionListener() {

            public void valueChanged(TreeSelectionEvent e) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) vTree.getLastSelectedPathComponent();
                if (node == null) {
                    return;
                }

                if (node.isLeaf()) {
                    Object nodeInfo = node.getUserObject();

                    System.out.println(nodeInfo);

                    if (!(((Node) nodeInfo).getData() instanceof MyPatch)) {
                        System.err.println(" Sorry this is not a patch node ");
                        return;
                    }
                    MyPatch patch = (MyPatch) ((Node) nodeInfo).getData();
                    System.out.println(patch);

                    //int chan = 0;
                    ShortMessage shm = new ShortMessage();
                    try {
                        shm.setMessage(ShortMessage.CONTROL_CHANGE, chan, 0,
                                patch.msb);
                        recv.send(shm, -1);
                        shm.setMessage(ShortMessage.CONTROL_CHANGE, chan, 0x20,
                                patch.lsb);
                        recv.send(shm, -1);
                        shm.setMessage(ShortMessage.PROGRAM_CHANGE, chan,
                                patch.prog, 0);
                        recv.send(shm, -1);
                    } catch (InvalidMidiDataException e1) {
                        e1.printStackTrace();
                    }

                    Config.setProperty("patch", Integer.toString(patch.hashCode()));
                    setChanged();
                    notifyObservers(patch.instrument);
                }
            }
        });

        return ret;

    }

    public void applyProperties(Properties prop) {


        String hashStr = prop.getProperty("patch");

        if (hashStr != null) {
            MyPatch patch = new MyPatch(Integer.parseInt(hashStr));
            vTree.select(patch);
        }

    }

    public JComponent getPanel() {
        return patchPanel;
    }
}
